package com.tofine.smarthome.adapter;

import android.os.Build;
import android.view.View;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.bignerdranch.expandablerecyclerview.ViewHolder.ParentViewHolder;
import com.tofine.smarthome.R;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author ddMax
 * @since 2016/5/22 19:32.
 */
public class CategoryViewHolder extends ParentViewHolder {

    private static final float INITIAL_POSITION = 0.0f;
    private static final float ROTATED_POSITION = 90f;

//    @Bind(R.id.swipe) SwipeLayout swipeLayout;
//    @Bind(R.id.delete) ImageView deleteView;
    @Bind(R.id.category_image) ImageView mCategoryImage;
    @Bind(R.id.category_title) TextView mCategoryTitle;
    @Bind(R.id.category_amount) TextView mCategoryAmount;
    @Bind(R.id.category_arrow) ImageView mCategoryArrow;

    public CategoryViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setExpanded(boolean expanded) {
        super.setExpanded(expanded);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            if (expanded) {
                mCategoryArrow.setRotation(ROTATED_POSITION);
            } else {
                mCategoryArrow.setRotation(INITIAL_POSITION);
            }
        }
    }

    @Override
    public void onExpansionToggled(boolean expanded) {
        super.onExpansionToggled(expanded);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            RotateAnimation rotateAnimation;
            if (expanded) { // rotate clockwise
                rotateAnimation = new RotateAnimation(
                        ROTATED_POSITION,
                        INITIAL_POSITION,
                        RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                        RotateAnimation.RELATIVE_TO_SELF, 0.5f
                );
            } else { // rotate counterclockwise
                rotateAnimation = new RotateAnimation(
                        -1 * ROTATED_POSITION,
                        INITIAL_POSITION,
                        RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                        RotateAnimation.RELATIVE_TO_SELF, 0.5f
                );
            }

            rotateAnimation.setDuration(200);
            rotateAnimation.setFillAfter(true);
            mCategoryArrow.startAnimation(rotateAnimation);
        }
    }
}
